#include "stdafx.h"
#include "OpenGl_Drawable.h"

/*
*  @brief Initialises all fonts
*/
void OpenGl_Drawable::initFonts()
{
    CWnd* pMainWindow = AfxGetMainWnd();
    HDC hdc = ::GetDC(pMainWindow->GetSafeHwnd());
    // Lucidia Console 10
    // Create font object
    m_FontArial_10.CreateFont(-10, 0, 0, 0, FW_BOLD, false, false, false, ANSI_CHARSET,
                              OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                              FF_DONTCARE | DEFAULT_PITCH, _T("Arial"));
    // Select as current object
    SelectObject(hdc, m_FontArial_10);
    // Create font bitmap
    wglUseFontBitmaps(hdc, 0, 255, OGL_DRAWABLE_FONT_ARIAL_10);
    // And bind it to a list
    glListBase(OGL_DRAWABLE_FONT_ARIAL_10);
    // Arial 10
    m_FontArial_15.CreateFont(-15, 0, 0, 0, FW_BOLD, false, false, false, ANSI_CHARSET,
                              OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                              FF_DONTCARE | DEFAULT_PITCH, _T("Arial"));
    SelectObject(hdc, m_FontArial_15);
    wglUseFontBitmaps(hdc, 0, 255, OGL_DRAWABLE_FONT_ARIAL_15);
    glListBase(OGL_DRAWABLE_FONT_ARIAL_15);
    // Lucidia Console 10
    m_FontLucidaConsole_10.CreateFont(-10, 0, 0, 0, 0, false, false, false, ANSI_CHARSET,
                                      OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                                      FF_DONTCARE | DEFAULT_PITCH, _T("Lucida Console"));
    SelectObject(hdc, m_FontLucidaConsole_10);
    wglUseFontBitmaps(hdc, 0, 255, OGL_DRAWABLE_FONT_LUCIDA_CONSOLE_10);
    glListBase(OGL_DRAWABLE_FONT_LUCIDA_CONSOLE_10);
}

/*
*  @brief Draw some text
*  @param Text - Text to be drawn
*  @param FontListBase - List base of the font
*  @param XPos - Position in x direction
*  @param YPos - Position in y direction
*  @param Red - Red part of the color
*  @param Green - Green part of the color
*  @param Blue - Blue part of the color
*/
void OpenGl_Drawable::drawText(const CStringW& Text,
                               OGL_DRAWABLE_FONT FontListBase,
                               double XPos, double YPos,
                               GLint Red,
                               GLint Green,
                               GLint Blue)
{
    // Check if the fonts are initialised yet, if not initialise them
    if(m_FontArial_10.GetSafeHandle() == NULL)
    {
        // Initialise fonts first
        initFonts();
    }

    // Disable 2D texturing
    glDisable(GL_TEXTURE_2D);
    // Specify the current color
    glColor3i(Red, Green, Blue);
    // Specify the position of the text
    glRasterPos2d(XPos, YPos);
    // Push the list base value
    glPushAttrib(GL_LIST_BIT);
    // Set a new list base value.
    glListBase(FontListBase);
    // Call the lists to draw the text.
    glCallLists((GLsizei)Text.GetLength(), GL_UNSIGNED_SHORT,
                (GLushort*)Text.GetString());
    glPopAttrib();
    // Reenable 2D texturing
    glEnable(GL_TEXTURE_2D);
}

// Default contructor
OpenGl_Drawable::OpenGl_Drawable(void)
{
}

// Default destructor
OpenGl_Drawable::~OpenGl_Drawable(void)
{
}
